package com.ruoyi.business.controller;

import com.ruoyi.business.domain.BilletTempHistory;
import com.ruoyi.business.service.IBilletTempHistoryService;
import com.ruoyi.business.vo.BaseWaterVo;
import com.ruoyi.business.vo.BilletTHistoryTemplateVo;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 钢坯温度历史记录Controller
 *
 * @author qiancheng
 * @date 2025-09-07
 */
@RestController
@RequestMapping("/business/temphistory")
public class BilletTempHistoryController extends BaseController
{
    @Autowired
    private IBilletTempHistoryService billetTempHistoryService;

    /**
     * 查询钢坯温度历史记录列表
     */
    @GetMapping("/list")
    public TableDataInfo list(BilletTempHistory billetTempHistory)
    {
        startPage();
        List<BilletTempHistory> list = billetTempHistoryService.selectBilletTempHistoryList(billetTempHistory);
        return getDataTable(list);
    }

    /**
     * 导出钢坯温度历史记录列表
     */
    @Log(title = "钢坯温度历史记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BilletTempHistory billetTempHistory)
    {
        List<BilletTempHistory> list = billetTempHistoryService.selectBilletTempHistoryList(billetTempHistory);
        ExcelUtil<BilletTempHistory> util = new ExcelUtil<BilletTempHistory>(BilletTempHistory.class);
        util.exportExcel(response, list, "钢坯温度历史记录数据");
    }

    /**
     * 获取钢坯温度历史记录详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(billetTempHistoryService.selectBilletTempHistoryById(id));
    }

    /**
     * 新增钢坯温度历史记录
     */
    @Log(title = "钢坯温度历史记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BilletTempHistory billetTempHistory)
    {
        return toAjax(billetTempHistoryService.insertBilletTempHistory(billetTempHistory));
    }

    /**
     * 修改钢坯温度历史记录
     */
    @Log(title = "钢坯温度历史记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BilletTempHistory billetTempHistory)
    {
        return toAjax(billetTempHistoryService.updateBilletTempHistory(billetTempHistory));
    }

    /**
     * 删除钢坯温度历史记录
     */
    @Log(title = "钢坯温度历史记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(billetTempHistoryService.deleteBilletTempHistoryByIds(ids));
    }

    /**
     * 获取钢坯温度历史曲线数据
     */
    @Anonymous
    @GetMapping("/curve")
    public AjaxResult getCurveData(int type) {
        BaseWaterVo data = billetTempHistoryService.getBilletTempHistoryData(type);
        return AjaxResult.success(data);
    }

    /**
     * 下载Excel模版
     */
    @Anonymous
    @GetMapping("/downloadTemplate")
    public void downloadTemplate(HttpServletResponse response)
    {
        ExcelUtil<BilletTHistoryTemplateVo> util = new ExcelUtil<>(BilletTHistoryTemplateVo.class);
        util.importTemplateExcel(response, "钢坯温度历史记录导入模版");
    }

    /**
     * Excel 导入钢坯温度历史记录
     *
     * @param file      Excel 文件
     * @param type      类型（1：升温 2：降温）
     * @param importDay 导入日期
     * @param startTime 开始时间
     * @param endTime   结束时间
     */
    @Anonymous
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file,
                                 @RequestParam("type") int type,
                                 @RequestParam("importDay") @DateTimeFormat(pattern = "yyyy-MM-dd") Date importDay,
                                 @RequestParam("startTime") String startTime,
                                 @RequestParam("endTime") String endTime) throws Exception
    {
        // 1. 读取 Excel 文件，转换为 VO 对象列表
        ExcelUtil<BilletTHistoryTemplateVo> util = new ExcelUtil<>(BilletTHistoryTemplateVo.class);
        List<BilletTHistoryTemplateVo> voList = util.importExcel(file.getInputStream());

        // 2. 调用 Service 进行业务处理（保存到数据库）
        String message = billetTempHistoryService.importBilletTempHistory(voList, type, importDay, startTime, endTime);

        // 3. 返回结果
        return AjaxResult.success(message);
    }

    /**
     * 查询钢坯温度历史记录（返回 BaseWaterVo，最多50个点，时间均分）
     */
    @Anonymous
    @GetMapping("/chartByParams")
    public AjaxResult chartByParams(
            @RequestParam("type") Integer type,
            @RequestParam("importDay") @DateTimeFormat(pattern = "yyyy-MM-dd") Date importDay,
            @RequestParam("startTime") String startTime,
            @RequestParam("endTime") String endTime) {

        BaseWaterVo baseWaterVo = billetTempHistoryService.getBilletTempHistoryChart(type, importDay, startTime, endTime);
        return AjaxResult.success(baseWaterVo);
    }


}
